<?php  
/*
	@Author: Ninh The Anh - 
	Email: ninhtheanh@yahooo.com, ninhtheanh@gmail.com
	Phone number: +84983.68.7979
*/
class clsControl
{ 	
	function __construct() 
	{
	}
	function render_control($type, $name, $value, $event = "", $css = "")
	{
		switch($type)
		{
			case "checkbox";
				render_checkbox($name, $value);
				break;
			case "text";
				render_hidden($name, $value);
				break;
			case "hidden";
				render_hidden($name, $value);
				break;		
			default:
				break;
		}
	}
	function render_checkbox($name, $value, $event = "")
	{
		$str = "<input type=\"checkbox\"" . ($value == 1 ? " checked=\"checked\"" : "") . " name=\"$name\" id=\"$name\" $event/>";
		return $str;
	}
	function render_text($name, $value, $style, $css = "", $event = "", $maxlength = "")
	{
		$str = "<input type=\"text\" name=\"$name\" id=\"$name\" value=\"$value\" style=\"$style\" class=\"$css\"/>";
		return $str;
	}
	function render_hidden($name, $value)
	{
		$str = "<input type=\"hidden\" name=\"$name\" id=\"$name\" value=\"$value\"/>";
		return $str;
	}
	function renderImage($img, $cbDelete = "", $w = '100%', $h = '100%', $class = '')
	{
		$str = "<img src=\"".DIR_WS_USER."$img\" border=\"0\" class=\"$class\">";
		if($img != "" && $cbDelete)
			$str .= "<br><input type=\"hidden\" name=\"h_$cbDelete\" id=\"h_$cbDelete\" value=\"$img\"> <label for=\"cb_$cbDelete\">Check for delete</label> <input type=\"checkbox\" name=\"cb_$cbDelete\" id=\"cb_$cbDelete\">";
		return $str;	
	}
	function render_image_admin($field_name, $field_value, $cbDelete = "", $w = '', $class = '')
	{
		$str = '<input type="file" name="f_'.$field_name.'"><br>';
		if($field_value[0] != "")
		{	
			$str .= "<br><img src=\"" . DIR_WS_USER.$field_value[0] . "\" border=\"0\" border=\"$class\"";
			if($w != "")
				$str .= " width=\"$w\"";
			$str .= ">";
			$str .= "<label for=\"$cbDelete\">Check to delete</label> <input type=\"checkbox\" name=\"$cbDelete\" id=\"$cbDelete\">";
		}
		$str .= "<input type=\"hidden\" name=\"hidden_old_image_$field_name\" id=\"hidden_old_image_$field_name\" value=\"" . implode(";", $field_value) . "\">";			
		return $str;	
	}
	function render_image_admin2($field_name, $field_value, $cbDelete = "", $w = '', $class = '')
	{
		$str = '<div style="margin:5px 0;"><input style="float:left;" type="file" name="f_'.$field_name.'">';
		if($field_value[0] != "")
		{	
			$str .= "<div style=\"background-color:#eee; display:inline-block;float:right; margin-right:116px;position:relative;\"><img src=\"" . DIR_WS_USER.$field_value[0] . "\" border=\"0\" border=\"$class\"";
			if($w != "")
				$str .= " width=\"$w\"";
			$str .= "><div style=\"float:left;position:absolute;bottom:0\"><input type=\"checkbox\" name=\"$cbDelete\" id=\"$cbDelete\" style=\"height:13px;margin:3px\" title=\"Delete current image?\"></div></div>";
		}
		$str .= "<input type=\"hidden\" name=\"hidden_old_image_$field_name\" id=\"hidden_old_image_$field_name\" value=\"" . implode(";", $field_value) . "\"></div>";			
		return $str;	
	}
	function render_file_admin($file, $file_name, $hidden_field = "", $cbDelete = "")
	{
		$str = '<input type="file" name="f_'.$file.'"><br>';
		if($file_name != "")
		{
			$str .= "<a href=\"".DIR_WS_USER."$file_name\" target=\"_blank\">". substr($file_name, strrpos($file_name, "/") + 1) ."</a>";
			if($file_name != "" && $hidden_field != "" && $cbDelete != "")
				$str .= "<br><input type=\"hidden\" name=\"$hidden_field\" id=\"$hidden_field\" value=\"$file_name\"> <label for=\"$cbDelete\">Check for delete</label> <input type=\"checkbox\" name=\"$cbDelete\" id=\"$cbDelete\">";
		}
		return $str;	
	}
	function render_ajax_upload_image($field_name, $field_value)
	{
		$str = "<div id=\"fileQueueUpload_$field_name\"></div>
		<input type=\"file\" name=\"$field_name\" id=\"$field_name\" />
		<div id=\"div_results_$field_name\"></div>
		<input type=\"hidden\" name=\"hidden_results_$field_name\" id=\"hidden_results_$field_name\" value=\"\" />";
		if($field_value[0] != "" && $this->check_img(DIR_WS_USER.$field_value[0]) == true)
		{
			$str .= "<br><img  id=\"img_hidden_results_".$field_name."\" src=\"" . DIR_WS_USER.$field_value[0] . "\" border=\"0\" width=\"120\" style=\"margin-bottom:20px;\">
			<!--<input type=\"checkbox\" name=\"ckDeleteImg_$field_name\" id=\"ckDeleteImg_$field_name\"> <label for=\"ckDeleteImg_$field_name\">Check to delete</label> -->";
		}
        else if(file_exists(DIR_WS_USER.$field_value[0]) == true)
        {
            $str .="<a href='".DIR_WS_USER.$field_value[0]."' target='_blank'>" .basename($field_value[0])."</a>";
        }
        else
        {
            $str .= "<br><img  id=\"img_hidden_results_".$field_name."\" src=\"" . DIR_WS_USER. "images/noimage.jpg". "\" border=\"0\" width=\"120\">";
        }
        
		if($field_value[0] == "")
		{
			$str .= "<br><img  id=\"img_hidden_results_".$field_name."\" src=\"" . DIR_WS_USER. "images/noimage.jpg". "\" border=\"0\" width=\"120\">
			<!--<input type=\"checkbox\" name=\"ckDeleteImg_$field_name\" id=\"ckDeleteImg_$field_name\"> <label for=\"ckDeleteImg_$field_name\">Check to delete</label> -->";
		}		
		$str .= "<input type=\"hidden\" name=\"hidden_old_image_$field_name\" id=\"hidden_old_image_$field_name\" value=\"" . implode(";", $field_value) . "\">";
		return $str;
	}
	function render_ajax_upload_image_lightbox($field_name, $field_value)
	{
		$str = "<div id=\"fileQueueUpload_$field_name\"></div>
		<input type=\"file\" name=\"$field_name\" id=\"$field_name\" />
		<div id=\"div_results_$field_name\"></div>
		<input type=\"hidden\" name=\"hidden_results_$field_name\" id=\"hidden_results_$field_name\" value=\"\" />";
		if($field_value[0] != "")
		{
			$str .= "<br><a id=\"". $field_name ."_lightbox\" href=\"". DIR_WS_USER.$field_value[0] ."\"><img width=\"120\" src=\"" . DIR_WS_USER.$field_value[0] . "\" border=\"0\"></a>
			<!--<input type=\"checkbox\" name=\"ckDeleteImg_$field_name\" id=\"ckDeleteImg_$field_name\"> <label for=\"ckDeleteImg_$field_name\">Check for delete</label>-->";
		}
		$str .= "<input type=\"hidden\" name=\"hidden_old_image_$field_name\" id=\"hidden_old_image_$field_name\" value=\"" . implode(";", $field_value) . "\">";
		$str .= '<script>$(document).ready(function(){$("a#'. $field_name .'_lightbox").lightBox(); }); </script>';
		return $str;
	}
	function render_ajax_upload_file($field_name, $field_value)
	{
		$str = "<div id=\"fileQueueUpload_$field_name\"></div>
		<input type=\"file\" name=\"$field_name\" id=\"$field_name\" />
		<div id=\"div_results_$field_name\"></div>
		<input type=\"hidden\" name=\"hidden_results_$field_name\" id=\"hidden_results_$field_name\" value=\"\" />
		<a href=\"" . DIR_WS_USER . $field_value . "\" target=\"_blank\">" . substr($field_value, strrpos($field_value, "/") + 1) . "</a><br />";
		if($field_value != "")			
			$str .= "<input type=\"checkbox\" name=\"ckDeleteImg_$field_name\" id=\"ckDeleteImg_$field_name\"> <label for=\"ckDeleteImg_$field_name\">Check for delete</label>";
		
		$str .= "<input type=\"hidden\" name=\"hidden_old_image_$field_name\" id=\"hidden_old_image_$field_name\" value=\"$field_value\">";
		return $str;
	}
    
    function check_img($img_url) {
        $img_formats = array("png", "jpg", "jpeg", "gif", "tiff");//Etc. . . 
        $path_info = pathinfo($img_url);

        if (in_array(strtolower(@$path_info['extension']), $img_formats)) {
           return true;
        }
        return false;
   }
}
?>